package com.jt.tenant.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.FreewayOrderAdapter;
import com.jt.tenant.conn.GetFreeWayOrder;
import com.jt.tenant.widget.MyListView;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

public class FreeWayOrderInfoActivity extends JTActivity implements View.OnClickListener{

    private TextView title_text;

    private TextView tv_order_mileage,tv_order_station,tv_order_time;

    private MyListView freeway_order_list;

    private String date;

    private List<GetFreeWayOrder.Item> orderList = new ArrayList<>();

    private FreewayOrderAdapter mAdapter;

    private double meilage = 0;

    private Integer station = 0;

    private long time = 0;

    private GetFreeWayOrder getFreeWayOrder = new GetFreeWayOrder(new AsyCallBack<GetFreeWayOrder.Info>(){

        @Override
        public void onSuccess(String toast, int type, GetFreeWayOrder.Info info) throws Exception {

            super.onSuccess(toast, type, info);

            if(info.msg.equals("000")){

                orderList.clear();

                orderList.addAll(info.freewayList);

                for(int i = 0;i<orderList.size();i++){

                    if(!orderList.get(i).meilage.equals("")&&!orderList.get(i).meilage.equals("null")) {

                        meilage += Double.parseDouble(orderList.get(i).meilage);

                    }

                    if(!orderList.get(i).station.equals("")&&!orderList.get(i).station.equals("null")) {

                        station += Integer.parseInt(orderList.get(i).station);

                    }

                    if(!orderList.get(i).end_time.equals("")&&!orderList.get(i).end_time.equals("null")) {

                        time += (Long.parseLong(orderList.get(i).end_time) - Long.parseLong(orderList.get(i).start_time)) / 1000 / 60;

                    }

                }

                if(time/60>0){

                    date = time/60+"小时"+time%60+"分";

                }else {

                    date = String.valueOf(time)+"分";

                }

                tv_order_mileage.setText(Tools.F_num(String.valueOf(meilage))+"公里");

                tv_order_station.setText(Tools.F_num(String.valueOf(station))+"个");

                tv_order_time.setText(date);

                mAdapter.notifyDataSetChanged();

            }else {

                UtilToast.show(context,info.msg);

            }
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_freewayorder_info);

        (title_text = (TextView) findViewById(R.id.tv_title_name)).setText("订单信息");

        tv_order_mileage = (TextView)findViewById(R.id.tv_order_mileage);

        tv_order_station = (TextView)findViewById(R.id.tv_order_station);

//        (tv_order_time = (TextView)findViewById(R.id.tv_order_time)).setOnClickListener(this);

        tv_order_time = (TextView)findViewById(R.id.tv_order_time);

        freeway_order_list = (MyListView) findViewById(R.id.freeway_order_list);

        mAdapter = new FreewayOrderAdapter(context,orderList);

        freeway_order_list.setAdapter(mAdapter);

    }

    @Override
    protected void onResume() {

        super.onResume();

        getFreeWayOrder.execute(this);

    }

    //    @Override
//    public void onClick(View view) {
//        super.onClick(view);
//
//        switch (view.getId()){
//
//            case R.id.tv_order_time:
//
//                Intent i = new Intent(context, FreeWayStateShowActivity.class)
//
//                        .putExtra("value","{\"order_num\":\"1482130341133\",\"in_time\":\"2016-12-19 14:52:27\",\"out_time\":\"2016-12-16 17:03:30\",\"mileage\":\"21\",\"in_name\":\"哈东收费站\",\"out_name\":\"宾西收费站\",\"price\":\"100\"}")
//
//                        .putExtra("message", "欢迎驶入,哈同高速,哈东收费站");
//
////                         .putExtra("message", "您已驶出,哈同高速,宾西收费站");
//
//                startActivity(i);
//
//                break;
//
//
//        }
//    }
}